
var Auto_Mod = false;
var root = document.querySelector(':root');

$(document).ready(function(){

    $.ajax({
        type: "GET",
        url : "AutoDemo?type=thread",
        success:function (data){
            if(data.data === true) {
                Auto_Mod = true;
                $("#vals").html("Close Auto Mod")
                root.setAttribute('style', '--Primary_Open: #ff007f');
            }
            InputInfo (data.time + " 获取线程状态成功");
        },
        error:function (){
            InputInfo ("获取线程状态失败")
        }
    })

    $.ajax({
        type: "GET",
        url : "AutoDemo?type=loadall",
        success:function (data){
            $('#User_Info').val(data.data);
            InputInfo (data.time + " 初始化数据源成功")
        },
        error:function (){
            InputInfo ("初始化数据源失败")
        }
    })

    self.setInterval("Time()",30000);
    InputInfo ("周期任务启动成功");
})

$("#Updata_btn").on("click",function(){

    var name = $('#name_input').val();
    var cookie = $('#Cook_input').val();
    var up_data = {"name": name, "cookie": cookie}

    $.ajax({
        type: "POST",
        url : "AutoDemo/",
        data: up_data,
        success:function(data){
            var msg = data.result;
            if(msg === "success"){
                switch (data.code){
                    case "100": InputInfo (data.time + " 信息提交成功"); break;
                    case "200": InputInfo (data.time + " 信息修改成功"); break;
                    default : break;
                }
                SetNullInput();
            }else if (msg === "error"){
                switch (data.code){
                    case "100": InputInfo (data.time + " Name和Cookie禁止为空"); break;
                    case "200": InputInfo (data.time + " 请关闭自动模式再提交"); break;
                    default : break;
                }
            }

            //location.reload(); //页面重载
        },
        error:function (){
            InputInfo (data.time + "提交失败");
        }
    });

})

$("#Up_userdata").on("click",function(){

    $.ajax({
        type: "GET",
        url : "AutoDemo?type=upall",
        success:function (data){
            $('#User_Info').val(data.data);
        },
        error:function (){
            InputInfo ("提交失败")
        }
    })
})

$("#Load_userinfo").on("click",function(){

    $.ajax({
        type: "GET",
        url : "AutoDemo?type=loadall",
        success:function (data){
            $('#User_Info').val(data.data);
        },
        error:function (){
            InputInfo ("获取失败")
        }
    })
})

$("#Auto_check").on("click",function(){

    if(Auto_Mod === false) {
        $.ajax({
            type: "GET",
            url : "AutoDemo?type=openauto",
            success:function (data){
                InputInfo (data.time + " 开启成功")
                Auto_Mod = true
                $("#vals").html("Close Auto Mod")
                root.setAttribute('style', '--Primary_Open: #ff007f');
            },
            error:function (){
                InputInfo ("开启失败")
            }
        })
    }
    else {
        $.ajax({
            type: "GET",
            url : "AutoDemo?type=closeauto",
            success:function (data){
                InputInfo (data.time + " 关闭成功")
                Auto_Mod = false
                $("#vals").html("Open Auto Mod")
                root.setAttribute('style', '--Primary_Open: #6d5dfc');
            },
            error:function (){
                InputInfo ("关闭失败")
            }
        })
    }


})

function InputInfo (msg){
    $('#OutPut_Info').val($('#OutPut_Info').val() + "【INFO】 " + msg + '\n');
}

function SetNullInput(){
    $('#name_input').val("");
    $('#Cook_input').val("");
}

function Time(){
    $.ajax({
        type: "GET",
        url : "AutoDemo?type=loadall",
        success:function (data){
            $('#User_Info').val(data.data);
        },
        error:function (){
            InputInfo ("周期任务异常")
        }
    })
}